Now that you know how to create graphics and visualizations in R, you are armed with powerful tools for scientific computing and analysis. With this power also comes great responsibility. Effective visualizations is an incredibly important aspect of scientific research and communication. There have been several books (see references) written about these principles. In class today we will be going through several case-studies trying to develop some expertise into making effective visualizations.
The worksheet questions for today are embedded into the class notes.
You can download this Rmd file here
Note, there will be very little coding in-class today, but I’ve given you plenty of exercises in the form of a supplemental worksheet (linked at the bottom of this page) to practice with after class is over.
Fundamentals of Data Visualization by Claus Wilke.
Visualization Analysis and Design by Tamara Munzner.
STAT545.com - Effective Graphics by Jenny Bryan.
ggplot2 book by Hadley Wickam.
Callingbull.org by Carl T. Bergstrom and Jevin West.
Write some notes here about what “effective visualizations” means to you. Think of elements of good graphics and plots that you have seen - what makes them good or bad? Write 3-5 points.
Question: Evaluate the strength of the claim based on the data: “German workers are more motivated and work more hours than workers in other EU nations.”
Very strong, strong, weak, very week, do not know
<< Cannot say the difference is significant without the significant level, number of people and standard deviation>>
Main takeaway: The difference between Germany and other countries is less than one hour–> x-axis is not appropriately chosen. Poor use of space in a limited length document. Missing the number of people involved–> is this representative??
Question: For the years this temperature data is displayed, is there an appreciable increase in temperature?
Yes, No, Do not know
<< y-axis is not appropriate, so it does not clearly show the change over time. There is barely any fluctuation from this plot. x-axis is not labelled>>
Main takeaway: x-axis is not labelled. y scale is not appropriate. Need scientific methodology to interpret the plot result–> two degree difference is appreciable–> the increase of temperature is real. Whether your axes should start at zero or not.
Question: Evaluate the strength of the claim based on the data: “Soon after this legislation was passed, gun deaths sharply declined.”
Very strong, strong, weak, very week, do not know
<< The upper part of the graph is red whichi s confusing. >>
Main takeaway: y-axis is upside down and it is hard to see whether there is an increase or decrease–> not easy to interpret the plot. Do not be creative with the color and axes.
Great resource for selecting the right plot: https://www.data-to-viz.com/ ; encourage you all to consult it when choosing to visualize data.
We will be filling these principles in together as a class
Instructions: Below is a code chunk that shows an effective visualization. First, copy this code chunk into a new cell. Then, modify it to purposely make this chart “bad” by breaking the principles of effective visualization above. Your final chart still needs to run/compile and it should still produce a plot.
How many of the principles did you manage to break?
Did you know that you can make interactive graphs and plots in R using the plotly library? We will show you a demo of what plotly is and why it’s useful, and then you can try converting a static ggplot graph into an interactive plotly graph.
This is a preview of what we’ll be doing in STAT 547 - making dynamic and interactive dashboards using R!
library(plotly)
library(tidyverse)
library(gapminder)
p <- ggplot(gapminder, aes(x=gdpPercap, y=lifeExp, color=continent)) +
geom_point()
# make interactive graph.
p %>%
ggplotly()
# plot_ly syntax
gapminder %>%
plot_ly(x = ~gdpPercap, y = ~lifeExp, color = ~continent, type="scatter", mode="markers")
Sys.setenv("plotly_username"="chengx32")
Sys.setenv("plotly_api_key"="jveX6MVe6BZf6koWWsxt")
api_create(p, filename="cm013_example")
You are highly encouraged to the cm013 supplemental exercises worksheet. It is a great guide that will take you through Scales, Colours, and Themes in ggplot. There is also a short guided activity showing you how to make a ggplot interactive using plotly.